package com.bupt.datacollect.service;

import com.bupt.datacollect.data.Packet;
import com.bupt.datacollect.dto.PacketDTO;
import com.bupt.datacollect.mapper.PacketMapper;
import com.bupt.datacollect.util.NumberFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PacketService {
    @Autowired
    private PacketMapper packetMapper;

    public List<PacketDTO> list(Integer meterId){
        List<PacketDTO> list=new ArrayList<>();
        List<Packet> packets=packetMapper.packet(meterId);
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        packets.forEach(o->{
            PacketDTO dto=new PacketDTO();
            BeanUtils.copyProperties(o,dto);
            dto.setTime(format.format(o.getTime()));
            dto.setPower(NumberFormat.formatFloat(o.getPower()/1000,2));
            dto.setEnergyConsumption(o.getEnergyConsumption()/3600000);
            dto.setCurrent(NumberFormat.formatFloat(o.getCurrent(),2));
            list.add(dto);
        });
        return list;
    }
}
